//
// Created by Asus on 2025/6/9.
//

#include <iostream>
using namespace std;

// 返回静态变量引用
int &test01() {
    int a = 10;
    cout << "a: " << (unsigned long long) &a << endl;
    return a;
}

// 返回静态变量引用
int &test02() {
    static int a = 10;
    cout << "a: " << (unsigned long long) &a << endl;
    return a;
}

void demo04() {
    //不能返回局部变量的引用
    int &ref = test01();
    // cout << "ref: " << ref << endl; // 报错SIGSEGV
    //如果函数做左值(有持久的内存地址，可被修改)，那么必须返回引用
    test02() = 30;
    int &ref2 = test02();
    cout << "ref2 = " << ref2 << endl;
}

class Base {
};

class Derived : public Base {
};

void demo05() {
    int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
    cout << "arr size:" << sizeof(arr) << endl;
    cout << "arr[0] size:" << sizeof(arr[0]) << endl;
    cout << "arr[0][0] size:" << sizeof(arr[0][0]) << endl;

    cout << "二维数组行数：" << sizeof(arr) / sizeof(arr[0]) << endl;
    cout << "二维数组列数：" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;

    cout << "二维数组首地址：" << arr << endl;
    cout << "二维数组第一行地址：" << arr[0] << endl;
    cout << "二维数组第二行地址：" << arr[1] << endl;

    // 二维数组第一个元素地址：0xf1f4bff6d0
    // 二维数组第二个元素地址：0xf1f4bff6d4 +4
    cout << "二维数组第一个元素地址：" << &arr[0][0] << endl;
    cout << "二维数组第二个元素地址：" << &arr[0][1] << endl;
    int *p = *arr;
    cout << p << endl;
    cout << "p[0]:" << p[0] << endl;
    cout << "p[5]:" << p[5] << endl;
    cout << "*p:" << *p << endl;
    // cout << "*p:" << *++p << endl;
    // cout << "*p:" << *++p << endl;
    // cout << "*p:" << *++p << endl;
    // arr 本质是指向第一行的指针，类型为 int(*)[3]
    int (*p2)[3] = arr;
    // cout << "p2:" << p2 << endl;
    // cout << "*p2:" << p2[0][0] << endl;
    // cout << "*p2:" << p2[1][0] << endl;
    // cout << "arr:" << arr << endl; // 解引用两次，输出: 1
    // cout << "*arr:" << *arr << endl; // 第一行地址
    // cout << "**arr:" << **arr << endl; // 解引用两次，输出: 1
    // cout << "*(arr + 1):" << *(arr + 1) << endl; // 第二行地址
    // cout << "*(*arr + 2)" << *(*arr + 2) << endl; // 第一行第3个元素，输出: 3
    // cout << "**(arr + 1):" << **(arr + 1) << endl; // 第二行第一个元素，输出: 4
    // cout << "*(*(arr + 1) + 2):" << *(*(arr + 1) + 2) << endl; // 第二行第三个元素
    // cout << "arr:" << arr << endl;
    // cout << "*arr:" << *arr << endl;
    // cout << "p2:" << p2 << endl;
    // cout << "*p2:" << *p2 << endl;
    // cout << "p:" << p << endl;
    // cout << "*p:" << *p << endl;
    // cout << &arr[0][0] << endl;
    // cout << &arr[0][1] << endl;
    // cout << &arr[0][2] << endl;
    // cout << &arr[1][0] << endl;
    // cout << &arr[1][1] << endl;
    // cout << &arr[1][2] << endl;
    cout << "sizeof(arr):" << sizeof(arr) << endl;
    cout << "sizeof(p2):" << sizeof(p2) << endl;
    cout << "sizeof(p):" << sizeof(p) << endl;
}

int main(int argc, char *argv[]) {
    Derived d;
    int num = 10;
    int &num_ref = num;
    int *num_ptr = &num;
    int *const num_ptr_ref = &num;
    cout << "num_ref = " << num_ref << endl;
    cout << "&num_ref = " << &num_ref << endl;
    cout << "num_ptr_ref = " << num_ptr_ref << endl;
    // demo04();
    demo05();
    return 0;
}
